package com.omesoft.hksnore.util.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.util.SparseArray;

import com.omesoft.hksnore.util.dbhelper.DBHelper4Sleep;
import com.omesoft.hksnore.util.dbhelper.SetData;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_Sleep;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Omesoft on 2017/3/13.
 */

public class MendaleSleepAllIfcImpl implements MendaleSleepAllIfc {
    DBHelper4Sleep db = null;
    String table = SetData.TABLE_NAME_MEDIX_PUB_SYNC_SLEEP;
    String table1 = SetData.TABLE_NAME_MEDIX_PUB_SYNC_SLEEPDETAIL;
    Context context;

    public MendaleSleepAllIfcImpl(Context context) {
        db = DBHelper4Sleep.getInstance(context, SetData.DATATBASE_NAME_MendaleSleep);
        this.context = context;
    }

    // memberID 在dto中已经设置
    @Override
    public void insertorupdate(Medix_Pub_Sync_Sleep dto) {
        db.InsertorUpdateMendaleSleepAll(table, dto);
    }

    @Override
    public void insertorupdate(List<Medix_Pub_Sync_Sleep> dtos) {
        // TODO Auto-generated method stub
        db.InsertorUpdateMendaleSleepAll(table, dtos);
    }

    // 已经设置memberID
    @Override
    public void update(int state, String ID, String RecordDate, int memberID) {
        db.updateWakeUpState(table, state, ID, RecordDate, memberID);

    }

    // 已经设置memberID
    @Override
    public Medix_Pub_Sync_Sleep findID(int postion, int memberID) {
        Cursor cursor = db.findSleepAllID(table, postion, memberID);
        Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
        while (cursor.moveToNext()) {
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dto;
    }

    // 已经设置memberID
    @Override
    public int findTotal(int MemberID) {
        // TODO Auto-generated method stub
        Cursor cursor = db.findTotal(table, MemberID);
        int total = 0;
        while (cursor.moveToNext()) {
            total = cursor.getInt(cursor.getColumnIndexOrThrow("Count"));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        Log.v("MendaleSleepAllIfcImpl", "findTotal::total=" + total);
        return total;
    }

    // 已经设置memberID
    @Override
    public int AllInBed(int MemberID) {
        // TODO Auto-generated method stub
        Cursor cursor = db.AllInBed(table, MemberID);
        int total = 0;
        while (cursor.moveToNext()) {
            total = cursor.getInt(cursor.getColumnIndexOrThrow("Count"));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return total;
    }

    // 已经设置memberID
    @Override
    public void updateTime(String ID, int InBedTime, int SleepTime, int DeepSleepTime, int SleepScore,
                           int memberID) {
        Log.v("updateTime", "1");
        db.updateTime(table, ID, InBedTime, SleepTime, DeepSleepTime, SleepScore, memberID);
        Log.v("updateTime", "2");
    }

    // 已经设置memberID
    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findDateTime(int memberID) {
        Cursor cursor = db.findDateTime(table, memberID);
        ArrayList<Medix_Pub_Sync_Sleep> dtos = new ArrayList<Medix_Pub_Sync_Sleep>();
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dtos.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dtos;
    }

    // 已经设置memberID
    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findDateTimeASC(int memberID) {
        Cursor cursor = db.findDateTimeASC(table, memberID);
        ArrayList<Medix_Pub_Sync_Sleep> dtos = new ArrayList<Medix_Pub_Sync_Sleep>();
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dtos.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dtos;
    }

    // 已经设置memberID
    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findMonthSleep(String Time, int memberID) {
        Cursor cursor = db.findMonthSleep(table, Time, memberID);
        ArrayList<Medix_Pub_Sync_Sleep> dtos = new ArrayList<Medix_Pub_Sync_Sleep>();
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setID(cursor.getString(cursor.getColumnIndexOrThrow("ID")));
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dtos.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dtos;
    }

    // 已经设置memberID
    @Override
    public Medix_Pub_Sync_Sleep findByID(String ID, int memberID) {
        Cursor cursor = db.findSleepByID(table, ID, memberID);
        Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
        while (cursor.moveToNext()) {
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dto.setSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("SleepTime")));
            dto.setDeepSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("DeepSleepTime")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
            dto.setDeviceName(cursor.getString(cursor.getColumnIndexOrThrow("DeviceName")));
            dto.setSourceType(cursor.getInt(cursor.getColumnIndexOrThrow("SourceType")));
            dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
            dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
            dto.setSnoreTime(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreTime")));
            dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
            dto.setBreathRate(cursor.getInt(cursor.getColumnIndexOrThrow("BreathRate")));
            dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dto;
    }

    // 已经设置memberID
    @Override
    public void deleteByID(String ID, int memberID, String updateTime) {
        // TODO Auto-generated method stub
        db.updateSleepByID(table, table1, ID, memberID, updateTime);
    }

    @Override
    public void deleteAllByID(int memberID) {
        db.deleteSleepBymenberID(table, memberID);

    }


    // 已经设置memberID
    @Override
    public int findPostionByTime(String Time, int memberID) {
        Cursor cursor = db.findPostionByTime(table, Time, memberID);
        int postion = 0;
        Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
        while (cursor.moveToNext()) {
            postion = cursor.getInt(cursor.getColumnIndexOrThrow("count"));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return postion;
    }

    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findSleepByMemberId(int memberID, String timestamp) {
        // TODO Auto-generated method stub
        Cursor cursor = db.findSleepByMemberId(table, memberID, timestamp);

        Log.e("cursor", "cursor 不为空" + cursor.toString());

        ArrayList<Medix_Pub_Sync_Sleep> dtos = new ArrayList<Medix_Pub_Sync_Sleep>();
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setID(cursor.getString(cursor.getColumnIndexOrThrow("ID")));
            dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dto.setSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("SleepTime")));
            dto.setDeepSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("DeepSleepTime")));
            dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
            dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
            dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
            dto.setQqHealth(cursor.getInt(cursor.getColumnIndexOrThrow("qqHealth")));
            dto.setDeviceName(cursor.getString(cursor.getColumnIndexOrThrow("DeviceName")));
            dto.setSourceType(cursor.getInt(cursor.getColumnIndexOrThrow("SourceType")));
            dto.setSnoreTime(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreTime")));
            dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
            dto.setBreathRate(cursor.getInt(cursor.getColumnIndexOrThrow("BreathRate")));
            dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));

            dtos.add(dto);
            Log.v("findSleepByMemberId", "dto:::" + dtos.toString());
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return dtos;
    }

    public ArrayList<Medix_Pub_Sync_Sleep> findSleepByMemberId(int memberID) {
        // TODO Auto-generated method stub
        Cursor cursor = db.findSleepByMemberId(table, memberID);
        ArrayList<Medix_Pub_Sync_Sleep> dtos = new ArrayList<Medix_Pub_Sync_Sleep>();
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setID(cursor.getString(cursor.getColumnIndexOrThrow("ID")));
            dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dto.setSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("SleepTime")));
            dto.setDeepSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("DeepSleepTime")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
            dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
            dto.setDeviceName(cursor.getString(cursor.getColumnIndexOrThrow("DeviceName")));
            dto.setSourceType(cursor.getInt(cursor.getColumnIndexOrThrow("SourceType")));
            dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
            dto.setBreathRate(cursor.getInt(cursor.getColumnIndexOrThrow("BreathRate")));
            dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
            dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
            dto.setSnoreTime(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreTime")));
            dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
            dtos.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        Log.e("hehe", "findSleepByMemberId: "+dtos.size());
        return dtos;
    }

    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findByQQHealth(int memberID) {
        // TODO Auto-generated method stub
        ArrayList<Medix_Pub_Sync_Sleep> arrayList = new ArrayList<Medix_Pub_Sync_Sleep>();
        Cursor cursor = db.findByQQHealth(table, memberID);
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setID(cursor.getString(cursor.getColumnIndexOrThrow("ID")));
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dto.setSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("SleepTime")));
            dto.setDeepSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("DeepSleepTime")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
            dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
            dto.setDeviceName(cursor.getString(cursor.getColumnIndexOrThrow("DeviceName")));
            dto.setSourceType(cursor.getInt(cursor.getColumnIndexOrThrow("SourceType")));
            arrayList.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return arrayList;
    }

    @Override
    public void updateQQHealth(int memberId, String sleepId, int qqHealth) {
        // TODO Auto-generated method stub
        db.updateQQHealthBySleepID(table, memberId, sleepId, qqHealth);
    }

    public ArrayList<Medix_Pub_Sync_Sleep> findSleepIDAndStartDate(int memberId) {
        ArrayList<Medix_Pub_Sync_Sleep> arrayList = new ArrayList<Medix_Pub_Sync_Sleep>();
        Cursor cursor = db.findSleepIDAndStartDate(memberId);
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            arrayList.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return arrayList;
    }

    @Override
    public SparseArray<Boolean> findDayByMonth(int memberID, String month) {
        // TODO Auto-generated method stub
        SparseArray<Boolean> array = new SparseArray<Boolean>();
        Cursor cursor = db.findDay4month(memberID, month);
        while (cursor.moveToNext()) {
            // 注意不要用getInt，会出现0值
            array.put(Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("StartDate"))), true);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return array;
    }

    @Override
    public String findNewestSleepID(int memberID) {
        // TODO Auto-generated method stub
        String temp = null;
        Cursor cursor = db.findNewestSleepID(memberID);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            temp = cursor.getString(0);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return temp;
    }

    @Override
    public String findNewestSleepIDByDate(int memberID, String Date) {
        // TODO Auto-generated method stub
        String temp = null;
        Cursor cursor = db.findNewestSleepIDByDate(memberID, Date);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            temp = cursor.getString(0);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return temp;
    }

    @Override
    public void updateTurnCount(String ID, int memberID, int turnCount) {
        // TODO Auto-generated method stub
        db.updateTurnCount(ID, memberID, turnCount);
    }

    @Override
    public void updateSnore(String sleepID, int memberID, int snoreTime, int snoreCount, int snoreFrequency,
                            int snore2AllActCount, int snore2AllTurnCount) {
        // TODO Auto-generated method stub
        db.updateSnore(sleepID, memberID, snoreTime, snoreCount, snoreFrequency, snore2AllActCount,
                snore2AllTurnCount);
    }

    @Override
    public ArrayList<Medix_Pub_Sync_Sleep> findByDateAndMemberId(String date, int memberID) {
        // TODO Auto-generated method stub
        ArrayList<Medix_Pub_Sync_Sleep> arrayList = new ArrayList<Medix_Pub_Sync_Sleep>();
        Cursor cursor = db.findByDateAndMemberId(table, date, memberID);
        while (cursor.moveToNext()) {
            Medix_Pub_Sync_Sleep dto = new Medix_Pub_Sync_Sleep();
            dto.setID(cursor.getString(cursor.getColumnIndexOrThrow("ID")));
            dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
            dto.setInBedTime(cursor.getInt(cursor.getColumnIndexOrThrow("InBedTime")));
            dto.setSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("SleepTime")));
            dto.setDeepSleepTime(cursor.getInt(cursor.getColumnIndexOrThrow("DeepSleepTime")));
            dto.setSleepScore(cursor.getInt(cursor.getColumnIndexOrThrow("SleepScore")));
            dto.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("Remark")));
            dto.setWakeupState(cursor.getInt(cursor.getColumnIndexOrThrow("WakeupState")));
            dto.setStartDate(cursor.getString(cursor.getColumnIndexOrThrow("StartDate")));
            dto.setEndDate(cursor.getString(cursor.getColumnIndexOrThrow("EndDate")));
            dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
            dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
            dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
            dto.setDeviceName(cursor.getString(cursor.getColumnIndexOrThrow("DeviceName")));
            dto.setSourceType(cursor.getInt(cursor.getColumnIndexOrThrow("SourceType")));
            dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
            dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
            dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
            dto.setBreathRate(cursor.getInt(cursor.getColumnIndexOrThrow("BreathRate")));
            dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
            dto.setSnoreTime(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreTime")));
            arrayList.add(dto);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return arrayList;
    }

    @Override
    public int AllTurnCount(int MemberID, String sleepId) {
        // TODO Auto-generated method stub
        Cursor cursor = db.AllTurnCount(table1, MemberID, sleepId);
        int total = 0;
        while (cursor.moveToNext()) {
            total = cursor.getInt(cursor.getColumnIndexOrThrow("Count"));
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return total;
    }

}
